<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册框</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/my_log_register.css">

</head>

<body>
    <!-- 注册框 -->
    <div id="" class="container shadow p-3 mb-5 rounded" style="font-family: cursive;">
        <div id="title">
            <h1 style="text-align: center; padding-top: 0.5rem;">注 册</h1>
        </div>
        <!-- 表单 -->
        <form id="registerForm" action="/registerDone" method="POST">
            <!-- 文本框 -->
            <div id="body_middle">

                <div class="row">
                    <div class="col">
                        <center>
                            <img src="images/用户名.png" alt="" style="width: 25px; height: 25px; position: absolute; left: 6rem; top: 0.5rem;">
                            <input type="text" class="form-control" id="username" name="username"
                                placeholder="用户名/手机号/邮箱" style="width: 50%; text-align: center;">
                            <!-- <small id="comment" class="form-text text-muted">{{msg}}</small> -->
                        </center>
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <center>
                            <img src="images/锁头.png" alt="" style="width: 25px; height: 25px; position: absolute; left: 6rem; top: 0.5rem;">
                            <input type="password" class="form-control" id="password" name="password"
                                placeholder="请输入密码" style="width: 50%; text-align: center">
                        </center>
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <center>
                            <img src="images/锁头.png" alt="" style="width: 25px; height: 25px; position: absolute; left: 6rem; top: 0.5rem;">
                            <input type="password" class="form-control" id="confirmpassword" name="confirmpassword"
                                placeholder="请确认密码" style="width: 50%; text-align: center">
                        </center>
                    </div>
                </div>
            </div>
            <br>
            <!--换行 -->
            <!-- <hr>底线 -->
            <!-- 按钮组件 -->
            <div style="text-align: center;">
                <button type="button" class="btn btn-outline-success" style="width: 5.5rem;"
                    onclick="jump()">注册</button>
                    &nbsp&nbsp&nbsp&nbsp&nbsp
                <!-- <button type="button" class="btn btn-outline-success"  style="width: 5.5rem;" name="reset" id="reset">重置</button>  -->
                <a href="#" onclick="login()">    
                    <button type="button" class="btn btn-outline-success" style="width: 5.5rem;" name="reset">取消</button>
                </a>
                <!--onclick="jumpOut()"-->
            </div>
    </div>
    </form>

    <!-- 注册提示框 -->
    <div class="col">
        <div class="alert alert-danger alert-dismissible fade show invisible text-center" role="alert">
            <strong>提示：</strong><label for="" id="msgId">莫高窟</label>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    </div>
    </div>

    <script src="js/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/register_btn.js"></script>
    <script src="js/register_input_focus.js"></script>

    <script>
        function login(){
            window.location.href='/login';
        }
        function jump() {
            // 获取表单数据
            let uid = $('#username').val();
            let pwd = $('#password').val();
            let confirmpwd = $('#confirmpassword').val();

            // 判断用户名、密码是否为空，为空则提示；不为空则向服务器提交
            if (uid.length > 0 && pwd.length > 0 && pwd === confirmpwd) {
                axios({
                    method: 'post',
                    url: '/registerDone',
                    data: $('#registerForm').serialize(),
                }).then(function (res) {
                    // axios工具下，服务器真正返回的数据在返回对象的data中
                    res = res.data;
                    if (res.code === 200) {
                        window.location.href = '/login';
                    } else {
                        alert_fn(res.msg);
                        console.log(res.msg);
                    }
                }).catch((err) => {
                    alert_fn(err);
                    console.log(err);
                });
            } else {
                alert_fn('用户名、密码不能为空，并且两次密码应该一致');
            }
        }

        // 包装了显示（以及自动隐藏）消息警告的动作
        function alert_fn(msg) {
            $('#msgId').text(msg);
            $('.alert').removeClass('invisible');
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
    </script>
</body>

</html>